<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\SkinList;
use api\wxapp\model\SkinListInfo;
use api\wxapp\model\Config;
use api\wxapp\model\Skin;
use api\wxapp\model\Integral;
use api\wxapp\model\User;
class SkinController extends ApiBaseController
{
    //答题规则
    public function index()
    {
        $info = Config::get(8);
        $this->success('请求成功!',$info);
    }
    //试题列表
    public function lst(Skin $skin)
    {
        $list = Skin::all()->each(function($item,$key){
            $item['answer'] = json_decode($item['answer']);
        });
        $this->success('请求成功!',$list);
    }
    //答题进度保存
    public function speed()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        !isset($params['skin_id']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['result']) ? $this->error('参数缺失!!!!') : '';
        Db::startTrans();
        try{
            //没有测试记录的id  为新测试
            if(!isset($params['skinlist_id'])){
                $res1 = SkinList::insertGetId(['uid'=>$params['uid']]);
                if(!$res1){
                    throw new \Exception('测试记录添加失败');
                }
                $skinlist_id = $res1;
            }else{
                $skinlist_id = $params['skinlist_id'];
            }
            //查询当前的记录传过来的试题id是否 已经答过
            $where = array();
            $where['skinlist_id'] = $skinlist_id;
            $where['skin_id'] = $params['skin_id'];
            //计算本次答题的分数
            $data['score'] = Config::where('name',$params['result'])->value('value');
            $find = SkinListInfo::where($where)->find();
            if($find){
                //当前记录  此题已经答过  说明是 返回上一题进行修改
                $data['result'] = $params['result'];
                $res = SkinListInfo::where('id',$find['id'])->update(['result'=>$params['result']]);
            }else{
                //添加测试的详细信息
                $data['skin_id'] = $params['skin_id'];
                $data['skinlist_id'] = $skinlist_id;
                $data['result'] = $params['result'];
                $res = SkinListInfo::insert($data);
            }
            if(!$res){
                throw new \Exception('测试记录详情添加失败');
            }
            //在进行判断 是否是最后一题 已经答题数和总题库 题数一致 答题完成计算分数
            if(Skin::count() == SkinListInfo::where('skinlist_id',$skinlist_id)->count()){
                $datas['status'] = 2;
                $datas['score'] = SkinListInfo::where('skinlist_id',$skinlist_id)->SUM('score');
                //计算当前分数是  那种状态
                $map[] = ['id','in',[5,6,7]];
                $list = Config::where($map)->select();
                
                if($list[0]['value'] > $datas['score']){
                    $datas['result'] = $list[0]['name'];
                }
                if($list[1]['value'] > $datas['score'] && $datas['score'] >= $list[0]['value']){
                    $datas['result'] = $list[1]['name'];
                }
                if($list[2]['value'] > $datas['score'] && $datas['score'] >= $list[1]['value']){
                    $datas['result'] = $list[2]['name'];
                }
                if($list[2]['value'] <= $datas['score']){
                    $datas['result'] = '完美';
                }
                $result = SkinList::where('id',$skinlist_id)->update($datas);
                if(!$result){
                    throw new \Exception('皮肤测试记录更新失败');
                }
                //判断  用户皮肤测试的时候  是否 获取过积分
                $ceshi = Integral::where(['uid'=>$params['uid'],'type'=>2])->find();
                if(!$ceshi){
                    //不存在  积分记录  添加
                    $data1['uid'] = $params['uid'];
                    $score = Config::where('id',11)->value('value');
                    $data1['integral'] = $score;
                    $data1['addtime'] = date('Y-m-d H:i:s',time());
                    $data1['time'] = date('Y-m-d',time());
                    $data1['type'] = 2;
                    $data1['status'] = 1;
                    $data1['remark'] = '皮肤测试获取积分';
                    $res4 = Integral::insert($data1);
                    if(!$res4){
                        throw new \Exception('积分记录表更新失败');
                    }
                    //用户表增加积分
                    $res5 = User::where('id',$params['uid'])->setInc('integral',$score);
                    if(!$res5){
                        throw new \Exception('用户积分更新失败');
                    }
                }
            }
            //记录添加完成
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('请求成功!',$skinlist_id);
    }
    ///皮肤测试结果
    public function result()
    {
        $params = $this->getParam();
        !isset($params['skinlist_id']) ? $this->error('参数缺失!!!!') : '';
        $data['info'] = SkinList::get($params['skinlist_id']);
        $data['sense'] = Config::get(9);
        $data['list'] = db::name('goods')->alias('t1')
            ->join('app_shopgoods t2','t2.goods_id = t1.id')
            ->field('t1.id,t1.name,t1.img,t2.price,t1.sales')
            ->where('t2.is_public',2)
            ->limit(2)
            ->select();
        $this->success('请求成功!',$data);
    }
}
